package com.lzq.badminton.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzq.badminton.dto.CourtTemplateDto;
import com.lzq.badminton.entity.CourtTemplate;
import com.lzq.badminton.mapper.CourtTemplateMapper;
import com.lzq.badminton.service.CourtTemplateService;
import com.lzq.badminton.utils.BeanCopyUtils;
import com.lzq.badminton.vo.CourtTemplateVo;
import com.lzq.badminton.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CourtTemplateServiceImpl extends ServiceImpl<CourtTemplateMapper, CourtTemplate> implements CourtTemplateService {

    @Autowired
    private CourtTemplateMapper courtTemplateMapper;

    @Override
    public ResponseResult addCourtTemplate(List<CourtTemplateDto> courtTemplateDtoList) {
        List<CourtTemplate> courtTemplates = BeanCopyUtils.copyBeanList(courtTemplateDtoList, CourtTemplate.class);
        for (CourtTemplate courtTemplate : courtTemplates) {
            courtTemplate.setCreateTime(LocalDateTime.now());
//            System.out.println(courtTemplate.toString());
            if(courtTemplateMapper.updateCourtTemplate(courtTemplate) != 1){
                System.out.println("更新失败!!!!!!!!!!!!!!!!!!!!!");
                //新增失败，尝试更新
                if(!save(courtTemplate)){
                    System.out.println("新增失败!!!!!!!!!!!!!!!!!!!!!");
                    return ResponseResult.errorResult(301, "场地模板新增或更新错误");
                };
            };
        }
        return ResponseResult.okResult();
    }

    @Override
    public List<CourtTemplateVo> listCourtTemplate(Long arenaId) {
        LambdaQueryWrapper<CourtTemplate> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CourtTemplate::getArenaId, arenaId);
        List<CourtTemplate> courtTemplateList = list(lambdaQueryWrapper);
        List<CourtTemplateVo> courtTemplateVos = BeanCopyUtils.copyBeanList(courtTemplateList, CourtTemplateVo.class);
        return courtTemplateVos;
    }

}
